package org.texteasy;

import java.util.HashSet;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.CharArrayBuffer;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.Groups;
import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
import android.provider.ContactsContract.CommonDataKinds.Phone;

public class ContactsAccessorSdk5 extends ContactsAccessor {

	
	static final int NUMBER_COLUMN_INDEX = 1;
	
	private  static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
        Contacts._ID, // 0
        Contacts.DISPLAY_NAME, // 1
        Contacts.STARRED, //2
        Contacts.TIMES_CONTACTED, //3
        Contacts.CONTACT_PRESENCE, //4
        Contacts.PHOTO_ID, //5
        Contacts.LOOKUP_KEY, //6
        Contacts.HAS_PHONE_NUMBER, //7
    };
	
	private String[] groupsProjection = new String[]{Groups._ID, Groups.SYSTEM_ID, Groups.TITLE, Groups.ACCOUNT_NAME, Groups.ACCOUNT_TYPE};
	
	@Override
	public Cursor queryContacts(ContentResolver contentResolver, String filter, long selectedGroupId) {
		
		if(filter == null || "".equals(filter.trim())){
			filter = "%";
		}else{
			filter = filter + "%";
		}
		
		Uri uri = ContactsContract.Data.CONTENT_URI;
		
		return contentResolver.query(uri, new String[]{
				Data._ID, Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE 
			}, Data.MIMETYPE + "=? AND " + Data.DISPLAY_NAME + " like '" + filter + "'", 
			new String[] {SynchronizeGroupsAndContacts.CONTENT_ITEM_TYPE}, Data.DISPLAY_NAME);
		
		/*return contentResolver.query(uri, CONTACTS_SUMMARY_PROJECTION, 
			Contacts.IN_VISIBLE_GROUP + "=1" + " AND " + Contacts.HAS_PHONE_NUMBER + "=1", 
			null, Contacts.DISPLAY_NAME);*/
	}

	@Override
	public Cursor queryGroups(ContentResolver contentResolver) {
		
		
		/*
		 * Example code to insert test group
		 * ContentValues values = new ContentValues();
    	values.put(android.provider.ContactsContract.Groups.TITLE, "MyGroup");
    	contentResolver.insert(android.provider.ContactsContract.Groups.CONTENT_URI, values);
    	*/
    	
    	/*
    	 * Example code to insert test group membership associations
    	 * ContentValues values = new ContentValues();
    	values.put(android.provider.ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID, 1);
    	values.put(android.provider.ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE, 
    			android.provider.ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);
    	values.put(android.provider.ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, 1);
    	contentResolver.insert(android.provider.ContactsContract.Data.CONTENT_URI, values); 
    	
    	values = new ContentValues();
    	values.put(android.provider.ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID, 2);
    	values.put(android.provider.ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE, 
    			android.provider.ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);
    	values.put(android.provider.ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, 1);
    	contentResolver.insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
    	*/
		
    	/* Used this to figure out how to query groupMembership. If you put some invalid value in
    	 * where clause and get android to throw an exception you can see the query used
    	 * Cursor tempCursor = contentResolver.query(android.provider.ContactsContract.Data.CONTENT_URI, 
			new String[]{GroupMembership.GROUP_ROW_ID,GroupMembership.GROUP_SOURCE_ID, 
			GroupMembership.DISPLAY_NAME, GroupMembership.RAW_CONTACT_ID}, 
			GroupMembership.MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE + "'", null,null);
    	
    	while(tempCursor.moveToNext()){
    		String groupRowId = tempCursor.getString(0);
    		String groupSourceId = tempCursor.getString(1);
    		String displayName = tempCursor.getString(2);
    		Long contactId = tempCursor.getLong(3);
    		Log.e("ContactsAccessor", "groupRowId: " + groupRowId + " groupSourceId: " + groupSourceId + 
				" displayName: " + displayName + " contactId: " + contactId);
    	}*/
    	
		return contentResolver.query(Groups.CONTENT_URI, groupsProjection, 
				null,null, Groups.TITLE);
	}
	
	@Override
	public Cursor queryContactsForPhoneNumbers(ContentResolver contentResolver,
			HashSet<Long> selectedIds) {
		
		StringBuffer buffer = new StringBuffer(" in(");
    	for(Long selectedId: selectedIds){
    		buffer.append(selectedId).
				append(","); 
    	}
		
    	String inStatement = buffer.substring(0, buffer.length() - 1) + ")";
    	
    	//Log.e(TAG, "before query createNewMessage");
    	
    	String[] projection = new String[] {Phone.RAW_CONTACT_ID, Phone.NUMBER, Phone.TYPE};
        
        Uri contacts = Phone.CONTENT_URI;
        
        return contentResolver.query(contacts, projection, 
    		Phone.RAW_CONTACT_ID + inStatement, null, null);
	}
	
	@Override
	public String getPhoneColumnIndex() {
		
		return Phone.NUMBER;
	}
	
	@Override
	public void setPhoneDetails(ContentResolver contentResolver, Cursor cursor,
			CharArrayBuffer numberBuffer, long contactId) {
		
		Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
		Uri dataUri = Uri.withAppendedPath(baseUri, Contacts.Data.CONTENT_DIRECTORY);
		
		Cursor c = null;
		try{
			c = contentResolver.query(dataUri, new String[] {Phone._ID, Phone.NUMBER,Phone.TYPE, 
					Phone.IS_SUPER_PRIMARY},Data.MIMETYPE + "=?", new String[] {Phone.CONTENT_ITEM_TYPE}, 
					null);
			if (c != null && c.moveToFirst()) {
				c.copyStringToBuffer(NUMBER_COLUMN_INDEX, numberBuffer);
			}
		}finally{
			if(c != null){
				c.close();
			}
		}
		
		
	}
	
}
